package com.bcu.cn.onlineexamsystem.studentmanage.service.impl;

import com.bcu.cn.onlineexamsystem.studentmanage.controller.StudentHomeDTO;
import com.bcu.cn.onlineexamsystem.studentmanage.mapper.StudentHomeMapper;
import com.bcu.cn.onlineexamsystem.studentmanage.service.StudentHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class StudentHomeServiceImpl implements StudentHomeService {
    
    @Autowired
    private StudentHomeMapper studentHomeMapper;
    
    @Override
    public StudentHomeDTO getHomeData(String studentId) {
        StudentHomeDTO homeData = new StudentHomeDTO();
        
        // 获取用户真实姓名
        String realName = studentHomeMapper.getRealNameByUserId(studentId);
        homeData.setRealName(realName != null ? realName : "同学");
        
        // 获取累计参加考试数量
        Integer totalExams = studentHomeMapper.getTotalExamsCount(studentId);
        homeData.setTotalExams(totalExams != null ? totalExams : 0);
        
        // 获取待参加考试数量
        Integer upcomingExams = studentHomeMapper.getUpcomingExamsCount(studentId);
        homeData.setUpcomingExams(upcomingExams != null ? upcomingExams : 0);
        homeData.setExamsCount(upcomingExams != null ? upcomingExams : 0);
        
        // 获取最近一次考试信息
        Map<String, Object> lastExamInfo = studentHomeMapper.getLastExamInfo(studentId);
        if (lastExamInfo != null) {
            homeData.setLastExamName((String) lastExamInfo.get("exam_name"));
            homeData.setLastExamCourse((String) lastExamInfo.get("course_name"));
            
            // 处理成绩信息
            Object studentScore = lastExamInfo.get("student_score");
            Object examTotalScore = lastExamInfo.get("exam_total_score");
            
            if (studentScore != null) {
                homeData.setLastExamScore(studentScore.toString());
            }
            if (examTotalScore != null) {
                homeData.setLastExamTotalScore(examTotalScore.toString());
            }
            
            // 处理时间信息
            Object submitTime = lastExamInfo.get("submit_time");
            if (submitTime instanceof LocalDateTime) {
                homeData.setLastExamTime((LocalDateTime) submitTime);
            }
        }
        
        // 获取最近题库更新信息
        Map<String, Object> latestQuestionInfo = studentHomeMapper.getLatestQuestionInfo();
        if (latestQuestionInfo != null) {
            homeData.setLatestQuestionCourse((String) latestQuestionInfo.get("course_name"));
            
            // 处理题目类型
            String questionType = (String) latestQuestionInfo.get("type");
            if (questionType != null) {
                switch (questionType) {
                    case "choice":
                        homeData.setLatestQuestionType("选择题");
                        break;
                    case "fill":
                        homeData.setLatestQuestionType("填空题");
                        break;
                    case "essay":
                        homeData.setLatestQuestionType("简答题");
                        break;
                    default:
                        homeData.setLatestQuestionType(questionType);
                        break;
                }
            }
            
            // 处理时间信息
            Object createTime = latestQuestionInfo.get("create_time");
            if (createTime instanceof LocalDateTime) {
                homeData.setLatestQuestionTime((LocalDateTime) createTime);
            }
        }
        
        return homeData;
    }
} 